﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;

namespace MinimalistFoodStreet.BackEnd.Server.Server.ResponseIqExtend
{
    public static class ResponseCommentIqExtend
    {
        public static IQueryable<ResponseCommentsDto> ToResponseCommentsIq(this IQueryable<Comment> iq)
        {
            return iq.Select(d => new ResponseCommentsDto
            {
                accountId = d.CAccountId,
                accountAvatar = d.CAccount.AAvatar,
                accountName = d.CAccount.AName,
                commentId = d.CCommentId,
                commentAccountName = d.CCommentId != null ? d.CComment.CAccount.AName : null,
                content = d.CContent,
                createTime = d.CCreateTime,
                id = d.CId,
                goodId = d.CGoodId,
                mainCommentId = d.CMainCommentId,
                rate = d.CRate,
                sources = d.CommentResources.Select(d => new ResponseCommentSourcesDto
                {
                    id = d.CrId,
                    extendName = d.CrExtendName,
                    fileName = d.CrFileName,
                    path = d.CrPath
                }).ToList(),
                replays = d.InverseCComment.Take(3).Select(d => new ResponseCommentsDto
                {
                    accountId = d.CAccountId,
                    accountAvatar = d.CAccount.AAvatar,
                    accountName = d.CAccount.AName,
                    commentId = d.CCommentId,
                    commentAccountName = d.CCommentId != null ? d.CComment.CAccount.AName : null,
                    content = d.CContent,
                    createTime = d.CCreateTime,
                    id = d.CId,
                    goodId = d.CGoodId,
                    mainCommentId = d.CMainCommentId,
                    rate = d.CRate,
                }).ToList(),
                replayTotal = d.InverseCComment.Count()
            });
        }
        public static IQueryable<ResponseCommentsDto> ToResponseCommentsIqNoReplayAndSource(this IQueryable<Comment> iq)
        {
            return iq.Select(d => new ResponseCommentsDto
            {
                accountId = d.CAccountId,
                accountAvatar = d.CAccount.AAvatar,
                accountName = d.CAccount.AName,
                commentId = d.CCommentId,
                commentAccountName = d.CCommentId != null ? d.CComment.CAccount.AName : null,
                content = d.CContent,
                createTime = d.CCreateTime,
                id = d.CId,
                goodId = d.CGoodId,
                mainCommentId = d.CMainCommentId,
                rate = d.CRate,
            });
        }
    }
}
